package com.qinglian.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qinglian.entity.Evaluate;
import com.qinglian.entity.Find;
import com.qinglian.entity.Like;
import com.qinglian.entity.User;
import com.qinglian.mapper.*;
import com.qinglian.service.IFindListService;
import com.qinglian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author augn
 * @since 2023-04-29
 */
@Service
public class FindListServiceImpl extends ServiceImpl<FindListMapper, Find> implements IFindListService {

    @Autowired
    private FriendMapper friendListMapper;

    @Autowired
    private EvaluateListMapper evaluateListMapper;

    @Autowired
    private LikeListMapper likeListMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FindListMapper mapper;

    @Override
    public Result<?> getFindList(Integer userId,Integer friendId,Integer type) {
        QueryWrapper<Find> wrapper = new QueryWrapper<>();
        if(type==0){
            // 获取好友列表
            List<Integer> friendIdList = friendListMapper.getFriendId(userId);
            friendIdList.add(userId);
            // 获取好友朋友圈
            wrapper.in("user_id", friendIdList);
        } else {
            // 获取指定用户朋友圈
            wrapper.eq("user_id", friendId);
        }
        List<Find> findList = mapper.selectList(wrapper);
        List<Map<String,Object>> finds = new ArrayList<>();
        Iterator<Find> iterator = findList.iterator();
        while (iterator.hasNext()) {
            Map<String,Object> find = new HashMap<>();
            Find findOne = iterator.next();

            // 获取好友朋友圈的点赞列表
            QueryWrapper<Like> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("find_id", findOne.getId());
            List<Like> likeLists = likeListMapper.selectList(wrapper1);
            Iterator<Like> iterator1 = likeLists.iterator();
            List<Map<String,Object>> likes = new ArrayList<>();
            Boolean iLike = false;
            while (iterator1.hasNext()){
                Map<String,Object> like = new HashMap<>();
                Like likeOne = iterator1.next();
                if(likeOne.getUserId()==userId){
                    iLike = true;
                }
                like.put("userId",likeOne.getUserId());
                String likeName = friendListMapper.getFriendNotes(userId, likeOne.getUserId());
                if(likeName==null||likeName.equals("")){
                    User likeUser = userMapper.selectById(likeOne.getUserId());
                    like.put("name",likeUser.getName());
                } else {
                    like.put("name",likeName);
                }
                likes.add(like);
            }

            // 获取好友朋友圈的评论列表
            QueryWrapper<Evaluate> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("find_id", findOne.getId()).orderByAsc("create_time");
            List<Evaluate> evaluateLists = evaluateListMapper.selectList(wrapper2);
            Iterator<Evaluate> iterator2 = evaluateLists.iterator();
            List<Map<String,Object>> evaluates = new ArrayList<>();
            while (iterator2.hasNext()){
                Map<String,Object> evaluate = new HashMap<>();
                Evaluate evaluateOne = iterator2.next();

                evaluate.put("userId",evaluateOne.getUserId());
                evaluate.put("content",evaluateOne.getContent());
                evaluate.put("evaId",evaluateOne.getId());
                String evaluateName = friendListMapper.getFriendNotes(userId, evaluateOne.getUserId());
                if(evaluateName==null||evaluateName.equals("")){
                    User evaluateUser = userMapper.selectById(evaluateOne.getUserId());
                    evaluate.put("name",evaluateUser.getName());
                } else {
                    evaluate.put("name",evaluateName);
                }
                evaluates.add(evaluate);
            }

            find.put("findId",findOne.getId());
            find.put("userId",findOne.getUserId());

            String friendNotes = friendListMapper.getFriendNotes(userId, findOne.getUserId());
            if(friendNotes==null||friendNotes.equals("")){
                User user1 = userMapper.selectById(userId);
                find.put("name",user1.getName());
            } else {
                find.put("name",friendNotes);
            }
            User user = userMapper.selectById(findOne.getUserId());
            find.put("image",user.getImage());
            find.put("content",findOne.getContent());
            List<String> pictures = (List<String>) JSON.parse(findOne.getPictures());
            find.put("pictures",pictures);
            find.put("creatTime",findOne.getCreateTime());
            find.put("iLike",iLike);
            find.put("likeList",likes);
            find.put("evaluateList",evaluates);
            finds.add(find);
        }
        return Result.success(finds);
    }

    @Override
    public Result<?> addFind(Find find) {
        int insert = mapper.insert(find);
        return Result.success(insert);
    }

    @Override
    public Result<?> delFind(Integer findId) {
        int i = mapper.deleteById(findId);
        return Result.success(i);
    }
}
